package com.prs.crm.action.crm;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.Linkman;
import com.prs.crm.domain.crm.MeetReport;
import com.prs.crm.domain.crm.MeetReportCritique;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.crm.LinkmanService;
import com.prs.crm.service.crm.MeetReportCritiqueService;
import com.prs.crm.service.crm.MeetReportService;

public class MeetReportAction extends BaseAction {
	private MeetReport meetReport;
	private MeetReportService meetReportService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private Collection<Linkman> linkmans;
	private MeetReportCritique meetReportCritique;
	private Customer customer;
	private Person person;

	private CustomerService customerService;
	private LinkmanService linkmanService;
	private MeetReportCritiqueService meetReportCritiqueService;

	private Collection<MeetReportCritique> meetReportCritiques;
	private File upload;
	private String uploadFileName;
	private String tradeIds;

	@Action("listRegCustomerMeetReport")
	public String listMyCustomer() {
		return SUCCESS;
	}

	@Action("listRegCustomerMeetReportByTrade")
	public String listRegCustomerMeetReportByTrade() {
		person = AppContext.getAccountContext().currentUser();
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Customer.class);
		criteria.createAlias("appoints", "a");
		criteria.add(Restrictions.eq("a.person", person));
		if (tradeIds != null) {
			if (!"all".equals(tradeIds)) {
				tradeIds = ReportUtils.URLDecode(tradeIds).trim();
				if (tradeIds.indexOf("trade") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("trade", "tr");
					criteria.add(Restrictions.eq("tr.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.indexOf("level") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("level", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(tradeIds)));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editMeetReport")
	public String editMeetReport() {
		linkmans = this.getLinkmanService().getLinkmanByCustomer(customer);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitMeetReport", results = {
			@Result(name = "success", type = "redirect", location = "listRegCustomerMeetReport"),
			@Result(name = "error", type = "chain", location = "editMeetReport") })
	public String submitMeetReport() {
		if (meetReport.getTitle() == null
				|| "".equals(meetReport.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (meetReport.getLinkman() == null) {
			addActionError("联系人不能为空");
			return ERROR;
		}
		meetReport.setCustomer(customer);
		this.getMeetReportService().saveMeetReport(meetReport, upload,
				uploadFileName);
		this.setPrompt("提交约见报告成功");
		return SUCCESS;
	}

	@Action("listDelegateMeetReport")
	public String listDelegateMeetReport() {
		return SUCCESS;
	}

	@Action("listDelegateMeetReportByTrade")
	public String listDelegateMeetReportByTrade() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetReport.class);
		criteria.add(Restrictions.eq("recorder", AppContext.getAccountContext()
				.currentUser()));
		criteria.addOrder(Order.desc("recordTime"));
		if (tradeIds != null) {
			if (!"all".equals(tradeIds)) {
				tradeIds = ReportUtils.URLDecode(tradeIds).trim();
				if (tradeIds.indexOf("trade") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("customer", "c");
					criteria.createAlias("c.trade", "tr");
					criteria.add(Restrictions.eq("tr.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.indexOf("level") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("customer", "c");
					criteria.createAlias("c.level", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(tradeIds)));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetReportService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listMeetReport")
	public String listMeetReport() {
		return SUCCESS;
	}

	@Action("listMeetReportByLevel")
	public String listMeetReportByLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetReport.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		if (tradeIds != null) {
			if (!"all".equals(tradeIds)) {
				tradeIds = ReportUtils.URLDecode(tradeIds).trim();
				if (tradeIds.indexOf("trade") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("customer", "c");
					criteria.createAlias("c.trade", "tr");
					criteria.add(Restrictions.eq("tr.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.indexOf("level") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("customer", "c");
					criteria.createAlias("c.level", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.split(",")[0].indexOf("dept") != -1) {
					Collection<Integer> personIds = new ArrayList<Integer>();
					for (String val : tradeIds.split(",")) {
						if (val.indexOf("dept") == -1) {
							personIds.add(Integer.parseInt(val));
						}
					}
					if (personIds.size() > 0) {
						criteria.createAlias("recorder", "rec");
						criteria.add(Restrictions.in("rec.id", personIds));
					}
				} else if (ToolKits.isInteger(tradeIds.split(",")[0])) {
					criteria.createAlias("recorder", "rec");
					criteria.add(Restrictions.eq("rec.id", Integer
							.parseInt(tradeIds.split(",")[0])));
				}
			}
		}

		this.setPagination(this.getMeetReportService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listMeetReportCritique")
	public String listMeetReportCritique() {
		return SUCCESS;
	}

	@Action("listMeetReportCritiqueByLevel")
	public String listMeetReportCritiqueByLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetReportCritique.class);
		criteria.addOrder(Order.desc("recordTime"));
		if (tradeIds != null) {
			if (!"all".equals(tradeIds)) {
				tradeIds = ReportUtils.URLDecode(tradeIds).trim();
				if (tradeIds.indexOf("trade") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("meetReport", "mr");
					criteria.createAlias("mr.customer", "c");
					criteria.createAlias("c.trade", "tr");
					criteria.add(Restrictions.eq("tr.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.indexOf("level") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("meetReport", "mr");
					criteria.createAlias("mr.customer", "c");
					criteria.createAlias("c.level", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.split(",")[0].indexOf("dept") != -1) {
					Collection<Integer> personIds = new ArrayList<Integer>();
					for (String val : tradeIds.split(",")) {
						if (val.indexOf("dept") == -1) {
							personIds.add(Integer.parseInt(val));
						}
					}
					if (personIds.size() > 0) {
						criteria.createAlias("meetReport", "mr");
						criteria.createAlias("mr.recorder", "rec");
						criteria.add(Restrictions.in("rec.id", personIds));
					}
				} else if (ToolKits.isInteger(tradeIds.split(",")[0])) {
					criteria.createAlias("meetReport", "mr");
					criteria.createAlias("mr.recorder", "rec");
					criteria.add(Restrictions.eq("rec.id", Integer
							.parseInt(tradeIds.split(",")[0])));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetReportCritiqueService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}

	@Action("viewMeetReport")
	public String viewMeetReport() {
		meetReportCritiques = this.getMeetReportCritiqueService()
				.getMeetReportCritique(meetReport);
		return SUCCESS;
	}

	@Action("listCritiqueMeetReport")
	public String listCritiqueMeetReport() {
		return SUCCESS;
	}

	@Action("listCritiqueMeetReportByLevel")
	public String listCritiqueMeetReportByLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetReport.class);
		criteria.addOrder(Order.desc("recordTime"));
		if (tradeIds != null) {
			if (!"all".equals(tradeIds)) {
				tradeIds = ReportUtils.URLDecode(tradeIds).trim();
				if (tradeIds.indexOf("trade") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("customer", "c");
					criteria.createAlias("c.trade", "tr");
					criteria.add(Restrictions.eq("tr.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.indexOf("level") != -1) {
					tradeIds = tradeIds.split(",")[0];
					criteria.createAlias("customer", "c");
					criteria.createAlias("c.level", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(tradeIds)));
				} else if (tradeIds.split(",")[0].indexOf("dept") != -1) {
					Collection<Integer> personIds = new ArrayList<Integer>();
					for (String val : tradeIds.split(",")) {
						if (val.indexOf("dept") == -1) {
							personIds.add(Integer.parseInt(val));
						}
					}
					if (personIds.size() > 0) {
						criteria.createAlias("recorder", "rec");
						criteria.add(Restrictions.in("rec.id", personIds));
					}
				} else if (ToolKits.isInteger(tradeIds.split(",")[0])) {
					criteria.createAlias("recorder", "rec");
					criteria.add(Restrictions.eq("rec.id", Integer
							.parseInt(tradeIds.split(",")[0])));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetReportService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editCritiqueMeetReport")
	public String editCritiqueMeetReport() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitCritiqueMeetReport", results = { @Result(name = "success", type = "redirect", location = "listCritiqueMeetReport") })
	public String submitCritiqueMeetReport() {
		meetReportCritique.setMeetReport(meetReport);
		this.getMeetReportCritiqueService().submitMeetReportCritique(
				meetReportCritique, upload, uploadFileName);
		this.setPrompt("提交约见点评成功");
		return SUCCESS;
	}

	@Action("viewCritiqueMeetReport")
	public String viewCritiqueMeetReport() {
		return SUCCESS;
	}

	public MeetReport getMeetReport() {
		return meetReport;
	}

	public void setMeetReport(MeetReport meetReport) {
		this.meetReport = meetReport;
	}

	public MeetReportService getMeetReportService() {
		return meetReportService;
	}

	public void setMeetReportService(MeetReportService meetReportService) {
		this.meetReportService = meetReportService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setLinkmans(Collection<Linkman> linkmans) {
		this.linkmans = linkmans;
	}

	public Collection<Linkman> getLinkmans() {
		return linkmans;
	}

	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}

	public LinkmanService getLinkmanService() {
		return linkmanService;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Customer getCustomer() {
		return customer;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Person getPerson() {
		return person;
	}

	public void setMeetReportCritiqueService(
			MeetReportCritiqueService meetReportCritiqueService) {
		this.meetReportCritiqueService = meetReportCritiqueService;
	}

	public MeetReportCritiqueService getMeetReportCritiqueService() {
		return meetReportCritiqueService;
	}

	public void setMeetReportCritiques(
			Collection<MeetReportCritique> meetReportCritiques) {
		this.meetReportCritiques = meetReportCritiques;
	}

	public Collection<MeetReportCritique> getMeetReportCritiques() {
		return meetReportCritiques;
	}

	public void setMeetReportCritique(MeetReportCritique meetReportCritique) {
		this.meetReportCritique = meetReportCritique;
	}

	public MeetReportCritique getMeetReportCritique() {
		return meetReportCritique;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getTradeIds() {
		return tradeIds;
	}

	public void setTradeIds(String tradeIds) {
		this.tradeIds = tradeIds;
	}

}
